package com.ss.android.newmedia.h;

import android.content.Context;

import com.bytedance.article.common.c.TTUtils;
import com.bytedance.common.plugin.PluginDepend;
import com.bytedance.common.utility.NetworkType;
import com.bytedance.frameworks.baselib.network.http.util.HttpQueryHelper;
import com.bytedance.frameworks.baselib.network.http.util.TaskInfo;
import com.bytedance.frameworks.baselib.network.http.util.d;
import com.bytedance.ttnet.utils.RetrofitUtils;
import com.ss.android.common.AppContext;
import com.ss.android.common.util.MultiProcessSharedProvider;
import com.ss.android.common.util.NetworkUtils;
import com.ss.android.common.util.ToolUtils;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

import inline.FileUtils;

public class PluginDependImpl implements PluginDepend {
    private static volatile PluginDependImpl instance;
    private AppContext mAppContext;

    private PluginDependImpl(AppContext appContext) {
        this.mAppContext = appContext;
    }

    public static PluginDependImpl getInstance(AppContext appContext) {
        if (instance == null) {
            synchronized (PluginDependImpl.class) {
                if (instance == null) {
                    instance = new PluginDependImpl(appContext);
                }
            }
        }
        return instance;
    }

    public final String getChannel() {
        return this.mAppContext != null ? this.mAppContext.getChannel() : null;
    }

    public final String getCurProcessName(Context context) {
        return ToolUtils.getCurProcessName(context);
    }

    public final String getFileContent(File file) {
        FileInputStream fis = FileUtils.getFileInputStream(file);
        return new String(FileUtils.inputStream2Bytes(fis));
    }

    public final String getPluginDownloadUrl(String host, Map<String,String> queryMap) {
        HttpQueryHelper httpQueryHelper = new HttpQueryHelper(host);
        httpQueryHelper.queryMap.putAll(queryMap);
        return httpQueryHelper.getEncodeQuery();
    }

    public final boolean downloadFile(String url, String arg13, String arg14) {
        int maxLenght = 8192;
        String v3 = null;
        d v5 = null;
        String v6 = null;
        TaskInfo v7 = null;
        List v8 = null;
        String[] v9 = null;
        int[] v10 = null;
        String v2 = arg13;
        String v4 = arg14;
        try {
            return RetrofitUtils.downloadFile(maxLenght, url, v2, v3, v4, v5, v6, v7, v8, v9, v10);
        } catch (Exception exception) {
            exception.printStackTrace();
            return false;
        }
    }

    public final String ssPluginConfigUrl() {
        if (this.mAppContext != null) {
            String ssPluginConfigUrl = MultiProcessSharedProvider
                    .getMultiProcessSharedPreference(this.mAppContext.getContext())
                    .getString("lite_plugin_check_url", "");
            if (android.support.design.a.isTextEmpty(ssPluginConfigUrl)) {
                return "http://s0z.pstatp.com/site/download/app/pl/news_lite/126/ss_plugin_config.json";
            } else if (!TTUtils.isValidUrl(ssPluginConfigUrl)) {
                return "http://s0z.pstatp.com/site/download/app/pl/news_lite/126/ss_plugin_config.json";
            } else {
                return ssPluginConfigUrl;
            }
        } else {
            return "http://s0z.pstatp.com/site/download/app/pl/news_lite/126/ss_plugin_config.json";
        }
    }

    public final boolean isNetworkAvailable(Context context) {
        return NetworkUtils.isNetworkAvailable(context);
    }

    public final String litePluginInfoSpName() {
        return "lite_plugin_update_info";
    }

    public final boolean allowNetwork(Context context) {
        NetworkType networkType = NetworkUtils.getNetworkType(context);
        return networkType == NetworkType.WIFI
                || networkType == NetworkType.MOBILE_4G
                || networkType == NetworkType.MOBILE_3G;
    }

    public final String ssLitePluginSpName() {
        return "ss_lite_plugins_pref";
    }
}

